#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project ：kuberCloudTest 
@File ：networkcard.py
@Author ：admin
@Date ：2024/3/28 10:11 
"""
from kuberCloudTest.cloud_resources.urls import CloudResourcesUrls
from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getDetail, _getOperationType
from kuberCloudTest.exception import KuberCloudException


class networkUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return networkUtils(accessToken=accessToken)


def getNetworkCardByVMId(accessToken, vmId, networkCardId=None, **kwargs):
    """
    通过虚拟机UUID查询网卡信息
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param kwargs: limit:
                   offset:
    :return:
    """
    content = get_utils(accessToken).cloud_http_request.get(CloudResourcesUrls.getNetworkCardByVMId.value,
                                                            {
                                                                'offset': kwargs.get('offset', OFFSET),
                                                                'limit': kwargs.get('limit', LIMIT),
                                                                'vmid': vmId
                                                            })
    if networkCardId:
        total = content.get('total', 0)
        content = get_utils(accessToken).cloud_http_request.get(
            CloudResourcesUrls.getNetworkCardByVMId.value, {'limit': total, 'offset': OFFSET})
        content = _getDetail(content, networkCardId, 'vmnicid')
    return content


def loadNetworkCard(accessToken, vmId, networkCardId, **kwargs):
    """
    云主机加载网卡
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param kwargs:
    :return:
    """
    # TODO:// 参数有误
    operationType = 1034
    data = _getOperationType(operationType, AsyncType.ASYNC.value)


def unloadNetworkCard(accessToken, vmId, networkCardId, **kwargs):
    """
    云主机卸载网卡
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param kwargs:
    :return:
    """
    operationType = 1035
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['networkCardId'] = networkCardId
    data['vmId'] = vmId

    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.unloadNetworkCard.value, data)


def getAvailableL3NetworkByVMId(accessToken, vmId, networkType: int = 0, **kwargs):
    """
    通过虚拟机UUID查询可用的三层网络
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkType: 网络类型
                        1：公有网络；2：私有网络；4：vpc网络
    :param kwargs: limit:
                   offset:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(CloudResourcesUrls.getAvailableL3NetworkByVMId.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'vmid': vmId,
                                                             'type': networkType,
                                                         })


def setNetworkCardMac(accessToken, vmId, networkCardId, mac):
    """
    设置网卡MAC地址
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param mac: 网卡MAC地址
    :return:
    """
    networkCard = getNetworkCardByVMId(accessToken, vmId, networkCardId=networkCardId).get('obj', None)
    if not networkCard:
        raise KuberCloudException('params error', 'networkCard [{}] not exist'.format(networkCard))
    operationType = 1038
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['vmid'] = vmId
    data['mac'] = mac
    data['networkCardId'] = networkCardId
    data['ip'] = networkCard['ip']
    data['ipv6'] = networkCard['ipv6']
    data['l3NetworkId'] = networkCard['nid']

    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.setNetworkCardMac.value, data)


def setNetworkCardQueue(accessToken, vmId, networkCardId, queue):
    """
    设置网卡队列
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param queue: 网卡队列
    :return:
    """
    networkCard = getNetworkCardByVMId(accessToken, vmId, networkCardId=networkCardId).get('obj', None)
    if not networkCard:
        raise KuberCloudException('params error', 'networkCard [{}] not exist'.format(networkCard))
    operationType = 1074
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['vmid'] = vmId
    data['vmnicid'] = networkCardId
    data['queues'] = queue
    data['mac'] = networkCard['mac']

    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.setNetworkCardQueue.value, data)


def setNetworkCardQos(accessToken, vmId, networkCardId,
                      uplinkBandwidth, uplinkBandwidthUnit, downlinkBandwidth, downlinkBandwidthUnit):
    """
    设置网卡QOS
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param uplinkBandwidth: 上行带宽
    :param uplinkBandwidthUnit: 上行带宽单位  Mbps|Gbps
    :param downlinkBandwidth: 下行带宽
    :param downlinkBandwidthUnit: 下行带宽单位    Mbps/Gbps
    :return:
    """
    networkCard = getNetworkCardByVMId(accessToken, vmId, networkCardId=networkCardId).get('obj', None)
    if not networkCard:
        raise KuberCloudException('params error', 'networkCard [{}] not exist'.format(networkCard))
    operationType = 1039
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['vmid'] = vmId
    data['networkCardId'] = networkCardId
    data['uplinkBandwidth'] = uplinkBandwidth
    data['uplinkBandwidthUnit'] = uplinkBandwidthUnit
    data['downlinkBandwidth'] = downlinkBandwidth
    data['downlinkBandwidthUnit'] = downlinkBandwidthUnit

    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.setNetworkCardQos.value, data)


def cancelNetworkCardQos(accessToken, vmId, networkCardId):
    """
    取消网卡QOS
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :return:
    """
    operationType = 1040
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['vmid'] = vmId
    data['networkCardId'] = networkCardId

    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.cancelNetworkCardQos.value, data)


def getAvailableNetworkCardIP(accessToken, networkCardId, **kwargs):
    """
    查询可用的IP
    :param accessToken: access token
    :param networkCardId: 网卡UUID
    :param kwargs:
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(CloudResourcesUrls.getAvailableNetworkCardIP.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'nid': networkCardId,
                                                         })


def setNetworkCardIP(accessToken, vmId, networkCardId, ip, ipv6=None):
    """
    设置网卡IP
    :param accessToken: access token
    :param vmId: 云主机UUID
    :param networkCardId: 网卡UUID
    :param ip: IP地址
    :param ipv6: IPv6地址
    :return:
    """
    networkCard = getNetworkCardByVMId(accessToken, vmId, networkCardId=networkCardId).get('obj', None)
    if not networkCard:
        raise KuberCloudException('params error', 'networkCard [{}] not exist'.format(networkCard))
    operationType = 1036
    data = _getOperationType(operationType, AsyncType.ASYNC.value)

    data['vmid'] = vmId
    data['networkCardId'] = networkCardId
    data['l3NetworkId'] = networkCard['nid']
    data['mac'] = networkCard['mac']
    data['ip'] = ip
    if ipv6:
        data['ipv6'] = ipv6
    return get_utils(accessToken).cloud_http_request.post(CloudResourcesUrls.setNetworkCardIP.value, data)
